package com.sense.kukoo.common.media.watcher

import android.os.Handler
import android.os.Looper
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.timer.TimerImpl
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.lib_logger.LoggerManager
import java.util.*

class UserWatcherImpl{

    private val TAG: String = javaClass.simpleName
    private val mHandler = Handler(Looper.getMainLooper())
    private val mWatchers:WeakHashMap<UserWatcher.MusicStateWatcher, Any> = WeakHashMap()
    private val mTimerWatchers:WeakHashMap<UserWatcher.TimerStateWatcher, Any> = WeakHashMap()
    private val defLocalMediaListItemBean = LocalMediaListItemBean.getDefMusic()

    private val defWatcher = object :UserWatcher.MusicStateWatcher{

        var mItemBean:LocalMediaListItemBean = defLocalMediaListItemBean
        var currentTimeInt:Int = 0
        var totalTimeInt:Int = 0
        var currentTime:String = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.media_player_time_zero)
        var totalTime:String = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.media_player_time_zero)
        var currentPercent: Int = 1
        var state:Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING
        var speed: Float = Repo.speed.get()

        override fun onMusicChanged(music: LocalMediaListItemBean) {
            mItemBean = music
            LoggerManager.d(TAG,"onMusicChanged ->\n${music.titleOfChapter()}=${music.getAudioUrl()}")
        }

        override fun onPlayTimeChanged(currentInt:Int, totalInt:Int, current:String, total:String) {
            currentTimeInt = currentInt
            totalTimeInt = totalInt
            currentTime = current
            totalTime = total
            LoggerManager.d(TAG,"onPlayTimeChanged: Int=${currentInt}/${totalInt},str=${current}/${total}")
        }

        override fun onPlayPercentChanged(percent: Int) {
            currentPercent = percent
            LoggerManager.d(TAG,"onPlayPercentChanged: percent=${percent}")
        }

        override fun onPlayStateChanged(state: Int) {
            this.state = state
            LoggerManager.d(TAG,"onPlayStateChanged state=${state}")
        }

        override fun onPlaySpeedChanged(speed: Float) {
            this.speed = speed
            LoggerManager.d(TAG,"onPlaySpeedChanged speed=${speed}")
        }

        override fun onPlayListChanged() {}

        fun sync(watcher: UserWatcher.MusicStateWatcher){
            watcher.onMusicChanged(mItemBean)
            watcher.onPlayTimeChanged(currentTimeInt, totalTimeInt, currentTime, totalTime)
            watcher.onPlayPercentChanged(currentPercent)
            watcher.onPlayStateChanged(state)
            watcher.onPlaySpeedChanged(speed)
            watcher.onPlayListChanged()
        }
    }

    private val defTimerWatcher = object :UserWatcher.TimerStateWatcher{

        private var type :Int = TimerImpl.TYPE_OFF
        private var left :Long = 0L
        private var leftStr :String = ""

        override fun onTimerChanged(type: Int, left: Long, leftStr:String) {
            this.type = type
            this.left = left
            this.leftStr = leftStr
            LoggerManager.d(TAG,"onTimerChanged type=${type}, left=${left}, leftStr=${leftStr}")
        }

        fun sync(watcher: UserWatcher.TimerStateWatcher){
            watcher.onTimerChanged(type, left, leftStr)
        }
    }

    init {
        mWatchers.set(defWatcher, null)
        mTimerWatchers.set(defTimerWatcher, null)
        dispatchStopPlay()
    }

    fun register(watcher: UserWatcher.MusicStateWatcher) {
        if (!mWatchers.containsKey(watcher)) {
            this.defWatcher.sync(watcher)
            mWatchers.set(watcher, null)
        }
    }

    fun unregister(watcher: UserWatcher.MusicStateWatcher) {
        mWatchers.remove(watcher)
    }

    fun dispatchStopPlay(){
        dispatchEvent{
            onMusicChanged(defLocalMediaListItemBean)
            val time = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.media_player_time_zero)
            onPlayTimeChanged(0,0,time, time)
            onPlayStateChanged(UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING)
        }
    }

    fun dispatchEvent(event:UserWatcher.MusicStateWatcher.() -> Unit){
        mHandler.post() {
            mWatchers.forEach {
                event(it.key)
            }
        }
    }

    fun registerTimerWatcher(watcher: UserWatcher.TimerStateWatcher){
        if (!mTimerWatchers.containsKey(watcher)) {
            this.defTimerWatcher.sync(watcher)
            mTimerWatchers.set(watcher, null)
        }
    }

    fun unregisterTimerWatcher(watcher: UserWatcher.TimerStateWatcher) {
        mTimerWatchers.remove(watcher)
    }

    fun dispatchTimerEvent(event:UserWatcher.TimerStateWatcher.() -> Unit){
        mHandler.post() {
            mTimerWatchers.forEach {
                event(it.key)
            }
        }
    }
}